package com.burgeon.backend.SHOPBACKEND.rest.find;

import com.burgeon.backend.Rest.base.RESTBase;
import com.burgeon.backend.SHOPBACKEND.entity.refund.RefundVO;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;


public class FindRefundData extends RESTBase {
    private final static String RANGE = "20";//分页数量
    private String ip;
    private String account;
    private String password;

    public FindRefundData(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }


    /**
     * 接收REP返回的字符串并进行数据处理
     *
     * @return
     */
    public ArrayList<RefundVO> getData(int start, String field, String message) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";
        String str = RESULT(Data(start,field,message), url);
        System.err.println(str);
        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception(str);
        }
        String count = new JSONArray(str).getJSONObject(0).getString("count");

        JSONArray jsonArray = new JSONArray(str.replace("-","")).getJSONObject(0).getJSONArray("rows");
        ArrayList<RefundVO> refundVOS = new ArrayList<RefundVO>();
        for (int i = 0; i < jsonArray.length(); i++) {
            RefundVO refundVO = new RefundVO();
            JSONArray array = jsonArray.getJSONArray(i);
            refundVO.setId(array.getString(0));
            refundVO.setCode(array.getString(1));
            refundVO.setGoodsCode(array.getString(2));
            refundVO.setGoodsName(array.getString(3));
            refundVO.setGoodsPrice(array.getString(4));
            refundVO.setNum(array.getString(5));
            refundVO.setTotPrice(array.getString(6));
            refundVO.setVipName(array.getString(7));
            refundVO.setVipPhone(array.getString(8));
            refundVO.setState(array.getString(9));
            refundVO.setCreateTime(array.getString(10));
            refundVO.setCount(count);

            refundVOS.add(refundVO);
        }

        return refundVOS;
    }

    /**
     * 准备向ERP传送字段拼接
     *
     * @return
     */
    private MultiValueMap Data(int y, String field, String message) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段

        columns.add("ID");//ID
        columns.add("'YC_RET_RETAIL_ID;DOCNO'");//code
        columns.add("'M_PRODUCT_ID;NAME'");//goodsCode
        columns.add("'M_PRODUCT_ID;VALUE'");//goodsName
        columns.add("'PRICELIST'");//goodsPrice
        columns.add("'QTY'");//num
        columns.add("'YC_RET_RETAIL_ID;TOT_AMT_ACTUAL'");//totPrice
        columns.add("'YC_RET_RETAIL_ID;C_VIP_ID;VIPNAME'");//vipName
        columns.add("'YC_RET_RETAIL_ID;C_VIP_ID;CARDNO'");//vipPhone
        columns.add("'YC_RET_RETAIL_ID;YC_RET_STATUS'");//state
        columns.add("CREATIONDATE");//createTime


        params.put("columns", columns);
        params.put("table", "YC_RET_RETAILITEM");//表名

        //设置查询规则（从第几条查询，每页显示多少条数）
        params.put("range", RANGE);
        params.put("count", "true");
        int r = Integer.valueOf(RANGE);
        params.put("start", String.valueOf(y * r));

        //设置排序规则
        HashMap orderby = new HashMap();
        orderby.put("column", "ID");
        orderby.put("asc", "false");
        ArrayList list = new ArrayList();
        list.add(orderby);

        params.put("orderby", list);
        HashMap paramss = new HashMap();

        paramss.put("column", "'"+field+"'");
        paramss.put("condition","'"+ message+"'");

        params.put("params", paramss);


        return DATA("Query", params, account, password);

    }

    public static void main(String[] args) {
        FindRefundData querryYCRetailout = new FindRefundData("39.104.73.78:5321", "nea@burgeon.com.cn", "burgeon5321");
        try {
            System.err.println(querryYCRetailout.getData(0,"YC_RET_RETAIL_ID;DOCNO","YCRT2012280006"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
